package com.personal.cuncunxing.module.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.personal.cuncunxing.R;


/**
 * Description: 滑动栏适配器
 * Created by kode on 2020/5/25.
 */
public class TabRVAdapter extends RecyclerView.Adapter<TabRVAdapter.ViewHolder> {

    private static final String TAG = "TabRVAdapter";
    //标题
    private String[] title;
    //已选择颜色
    private int selectedColor = R.color.colorHome_tab_text_bg;
    //正常颜色
    private int normalColor = R.color.colorHome_tab_text_normal;
    //标记已选择
    private int selected = 0;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public TabRVAdapter(Context context, String title[]) {
        this.context = context;
        this.title = title;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_tab, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    //有多少个Item就调用多少次,并且点击一次调用一次
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        //初始加载
        if (position == selected) {
            holder.recommend_tv.setText(title[position]);
            holder.recommend_tv.setTextColor(context.getResources().getColor(selectedColor));
            holder.recommend_line.setVisibility(View.VISIBLE);
        } else {
            holder.recommend_tv.setText(title[position]);
            holder.recommend_tv.setTextColor(context.getResources().getColor(normalColor));
            holder.recommend_line.setVisibility(View.INVISIBLE);
        }
        if (onItemClickListener != null) {
            holder.recommend_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.recommend_ll, holder.getLayoutPosition());
                    selected = position;
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return title.length;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout recommend_ll;
        TextView recommend_tv;
        View recommend_line;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            recommend_ll = itemView.findViewById(R.id.recommend_ll);
            recommend_tv = itemView.findViewById(R.id.recommend_tv);
            recommend_line = itemView.findViewById(R.id.recommend_line);
        }
    }

    public void setSelected(int position) {
        selected = position;
        notifyDataSetChanged();
    }

    interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
